<?php

declare(strict_types=1);

namespace App\CustHub\Service;

use App\CustHub\Constants\ContactTaskCenterTyperConstants;
use App\CustHub\Constants\TagModuleConstants;
use App\CustHub\Dto\ContactTcContentDto;
use App\CustHub\Mapper\ChContactMapper;
use App\CustHub\Mapper\ChSmartTagRuleMapper;
use App\CustHub\Model\ChContact;
use App\CustHub\Model\ChContactCorpTag;
use App\CustHub\Model\ChContactSmartTag;
use App\CustHub\Model\ChSmartTagRule;
use Hyperf\Di\Annotation\Inject;

class AutoAssignSmartTagService
{

    #[Inject]
    protected ChContactMapper $contactMapper;

    #[Inject]
    protected ChSmartTagRuleMapper $smartTagRuleMapper;

    /**
     * 触发生命周期标签
     * @param array $contactIds
     * @param array $tagIds
     * @return void
     */
    public function triggerLifecycleTagScheme(int $mainUid,array $contactIds,array $tagIds): void
    {
        $ruleList = $this->smartTagRuleMapper->getActiveRules($mainUid,TagModuleConstants::LIFE_CYCLE);
        foreach ($ruleList as $ruleItem){
            $rule = $ruleItem['rule'];

            foreach ($rule as $item){
                $tagId = $item['tag_id'];

                if(empty($item['corp_tag']) && empty($item['smart_tag'])){
                    continue;
                }

                foreach ($contactIds as $contactId){
                    $smartTagExists = ChContactSmartTag::query()
                        ->where('contact_id',$contactId)
                        ->where('smart_tag_id',$tagId)
                        ->exists();
                    if($smartTagExists){
                        continue;
                    }

                    $contactInfo = $this->contactMapper->findById($contactId);
                    $condOperator = $item['cond_operator'];

                    $corpMatchResult = true;
                    if (!empty($item['corp_tag'])) {
                        $corpTagCond = $item['corp_tag'];
                        $corpTagIds = $contactInfo->corpTags()->pluck('id')->toArray();
                        $corpMatchResult = $this->checkCondition((int)$corpTagCond['match_type'],$corpTagIds, $corpTagCond['ids']);
                    }

                    $smartMatchResult = true;
                    if (!empty($item['smart_tag'])) {
                        $smartTagCond = $item['smart_tag'];
                        $smartTagIds = $contactInfo->smartTags()->pluck('id')->toArray();
                        $smartMatchResult = $this->checkCondition((int)$smartTagCond['match_type'],$smartTagIds, $smartTagCond['ids']);
                    }

                    if ($condOperator == 'or') {
                        $matchResult = $corpMatchResult || $smartMatchResult;
                    } else {
                        $matchResult = $corpMatchResult && $smartMatchResult;
                    }

                    if($matchResult){
                        $content = new ContactTcContentDto();
                        $content->setContactIds([$contactId]);
                        $content->setTagIds([$tagId]);
                        container()->get(ChContactTaskCenterService::class)->addTask($contactInfo->uid,$contactInfo->main_uid,ContactTaskCenterTyperConstants::PUT_ON_SMART_TAG,$content);
                    }
                }
            }
        }

    }

    private function checkCondition(int $matchType,array $contactTagIds,array $tagIds): bool
    {
        if ($matchType == 0) {
            // 任意一个
            return !empty(array_intersect($tagIds, $contactTagIds));
        } else {
            // 全部
            return empty(array_diff($tagIds, $contactTagIds));
        }
    }
}